package milu.servlets;

import java.io.IOException;
import java.util.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.*;

import javax.jdo.PersistenceManager;
import javax.jdo.FetchGroup;

import milu.models.*;
import milu.*;

public class Index extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {

        PersistenceManager pm = Datastore.getPersistenceManager();
        String query = "select from " + Recipe.class.getName()+ " order by created desc";
        List<Recipe> recipes = (List<Recipe>)pm.newQuery(query).execute();
        LOG.info("executed query");
        LOG.info("recipes: "+recipes);

        req.setAttribute("recipes", recipes);

        forward(req, resp, "index.jsp");
	}

	/**
	 * Forwards request and response to given path. Handles any exceptions
	 * caused by forward target by printing them to logger.
	 * 
	 * @param request 
	 * @param response
	 * @param path 
	 */
	protected void forward(HttpServletRequest request,
			HttpServletResponse response, String path) {
		try {
			RequestDispatcher rd = request.getRequestDispatcher(path);
			rd.forward(request, response);
		} catch (Throwable tr) {
		}
	}
}
